﻿#include"stdafx.h"
#include"SQLexpression.h"

bool SqlEx::ForUpdate() {
	string db = CStringTostring(pSocket->cur_db);
	if (db == "") {
		pSocket->SendMSG("Please choose a database !");
		return 0;
	}
	string msg = "";
	smatch sm;
	vector<insert_model>fields;
	vector<struct_where>wh;
	regex re1("(?<=update ).+(?= set)");
	regex re2("(?<=set ).+(?= where)");
	regex re3("(?<=where ).+(?=END)");
	string tname = "";
	if (regex_search(Ex, sm, re1)) {
		tname = sm.str();
	}
	else {
		pSocket->SendMSG( "请检查update与set语句间是否存在错误!!" );
		return false;
	}
	if (!authority->Auth_Judge(user_host, db, tname)) {
		pSocket->SendMSG("You do not have permission to  update the table !");
		return false;
	}
	if (regex_search(Ex, sm, re2)) {
		string ss = sm.str();
		ss = trimAll(ss, " ");
		vector<string>	setInfo = split(ss,",");
		for (unsigned int i = 0; i < setInfo.size(); i++) {
			insert_model m;
			vector<string>result = simpleParse(setInfo[i]);
			m.key = result[0];
			m.value = result[2];
			fields.push_back(m);
		}
	
	}
	else {
		pSocket->SendMSG("请检查set字段是否存在错误！！");
		return false;
	}
	if (regex_search(Ex, sm, re3)) {
		string test = sm.str();
		test = trimAll(test, " ");
		where(test, wh);
	}
	else {
		pSocket->SendMSG("请检查where语句之后是否存在错误!!");
		return false;
	}
	serverDB->Table_update(tname.c_str(),db,fields,wh,msg);
	pSocket->SendMSG(msg);
	return true;
}